<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="gradeRules" :model="gradeForm" label-position="left" label-width="140px">
      <el-form-item label="编码" prop="code">
        <el-input v-model="gradeForm.code" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="gradeForm.title" />
      </el-form-item>
      <el-form-item label="图片">
        <Upload v-model="gradeForm.avatar" />
      </el-form-item>
      <el-form-item label="最小积分">
        <el-input v-model="gradeForm.minimum" />
      </el-form-item>
      <el-form-item label="最大积分">
        <el-input v-model="gradeForm.maximum" />
      </el-form-item>
      <el-form-item label="介绍">
        <el-input v-model="gradeForm.content" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入产品介绍" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="gradeForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="gradeForm.status.value === 5" type="danger" @click="handleDelete(gradeForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveGrade, removeGradeById, getGrade } from '@/api/customer/grade'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/SingleImage' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      listQuery: {
        start: 1,
        perpage: 20,
        keyword: undefined,
        sort: '+id'
      },
      gradeForm: {
        code: '',
        title: '',
        avatar: '',
        maximum: '',
        minimum: '',
        remark: '',
        content: '',
        status: { label: '', value: 1 }
      },
      gradeRules: []
    }
  },
  created() {
    let gradeId = this.$route.query && this.$route.query.gradeId
    if (gradeId !== undefined) {
      gradeId = parseInt(gradeId)
      if (!isNaN(gradeId)) {
        this.listLoading = true
        const formData = {
          'gradeId': gradeId
        }
        getGrade(formData).then(response => {
          if (response.status === 200) {
            this.gradeForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(gradeId)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'grade' })
    },
    createData() {
      this.listLoading = true

      const _gradeForm = this.gradeForm
      _gradeForm.status = _gradeForm.status.value
      if (_gradeForm.identity > 0) {
        _gradeForm.gradeId = _gradeForm.identity
      }
      saveGrade(_gradeForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'grade' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.$confirm('确实要执行此操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        const formData = {
          'gradeId': row.identity
        }
        removeGradeById(formData).then(response => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'group' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
